package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

public class StudentDao {

    //创建新数组
    private static Student[] students = new Student[5];

    public boolean addStudent(Student stu) {
        int index = -1;

        for (int i = 0; i < students.length; i++) {
            if (students[i] == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            students[index] = stu;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return students;
    }

    public void deleteStudent(String delId) {

        int index = getIndex(delId);
        students[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        //遍历数组
        for (int i = 0; i < students.length; i++) {
            Student stu = students[i];
            if (stu != null && id.equals(stu.getId())) {
                index = i;
            }
        }
        return index;
    }

    public void updateStudent(Student newStu, String updateId) {
        int index = getIndex(updateId);

        students[index] = newStu;
    }
}
